//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.web.riart.message.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.framework.common.ITimeService;
import nc.bs.logging.Logger;
import nc.itf.uap.pf.IApproveButtonService;
import nc.itf.uap.pf.IPFMetaModel;
import nc.vo.ml.MultiLangUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pf.button.ApproveButtonSettingVO;
import nc.vo.pub.BusinessException;
import nccloud.commons.collections.MapUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.impl.platform.common.util.StringUtils;
import nccloud.itf.ext.ILcSSOUrlExt;
import nccloud.message.itf.IApproveMessageQueryService;
import nccloud.message.itf.IMessageQueryService;

public class ListAction extends MessageQueryAction implements ICommonAction {
    public ListAction() {
    }

    public Object doAction(IRequest paramIRequest) {

        ILcSSOUrlExt iLcSSOUrlExt = (ILcSSOUrlExt)ServiceLocator.find(ILcSSOUrlExt.class);

        String jsonStr = paramIRequest.read();
        JSONObject params = JSON.parseObject(jsonStr);
        String msgType = "" + params.get("msgType");
        String isread = params.getString("isread");
        if (StringUtils.isBlank(msgType) || "null".equals(msgType)) {
            msgType = "approve";
            params.put("msgType", msgType);
        }

        String userId = "";
        userId = SessionContext.getInstance().getClientInfo().getUserid();
        Class<IMessageQueryService> cls = this.getItfByMsgType(msgType);
        IMessageQueryService iMessageQueryService = (IMessageQueryService)ServiceLocator.find(cls);
        JSONObject result = new JSONObject();
        params.put("userId", userId);

        try {
            params.put("group", SessionContext.getInstance().getClientInfo().getPk_group());
            Set<String> billTypeSet = new HashSet();
            List<JSONObject> msgs = iMessageQueryService.queryNCMessages(params);
            if (!"approve".equals(msgType)) {
                Iterator var24 = msgs.iterator();

                while(var24.hasNext()) {
                    JSONObject json = (JSONObject)var24.next();
                    billTypeSet.add(json.getString("billtype"));
                }
            } else {
                List<String> billtypeList = new ArrayList();
                List<String> billIdList = new ArrayList();
                List<Map<String, String>> pointMapList = new ArrayList();
                Map<String, String> pointMap = null;
                if (msgs != null && msgs.size() > 0) {
                    Iterator var16 = msgs.iterator();

                    while(true) {
                        if (!var16.hasNext()) {
                            iMessageQueryService.queryBillCommitUserInfo(billtypeList, billIdList, msgs);
                            if (!"approve".equals(msgType)) {
                                break;
                            }

                            IPFMetaModel pFMetaModelImpl = (IPFMetaModel)ServiceLocator.find(IPFMetaModel.class);
                            pFMetaModelImpl.setApproveMessage(msgs);
                            IApproveMessageQueryService approveMessageQueryService = (IApproveMessageQueryService)ServiceLocator.find(IApproveMessageQueryService.class);
                            approveMessageQueryService.setApprovePointStatus(pointMapList, msgs);
                            if ("N".equals(isread) || StringUtils.isBlank(isread)) {
                                approveMessageQueryService.setMsgAttNumber(billIdList, billtypeList, msgs);
                            }
                            break;
                        }

                        JSONObject json = (JSONObject)var16.next();
                        pointMap = new HashMap();
                        String billid = json.getString("billid");
                        String pk_checkflow = json.getString("pk_detail");
                        String flownodeid = json.getString("current_act");
                        billIdList.add(billid);
                        billtypeList.add(json.getString("billtype"));
                        billTypeSet.add(json.getString("billtype"));
                        String detail = json.getString("detail");
                        if (detail != null) {
                            String newDetail = this.getParam(detail, "button", "@");
                            if (newDetail != null && !"".equals(newDetail.trim())) {
                                json.put("detail", newDetail);
                            } else if (detail.indexOf("{") == 0 && detail.lastIndexOf("}") == detail.length() - 1) {
                                json.put("detail", detail);
                            }
                        }

                        pointMap.put("billid", billid);
                        pointMap.put("pk_checkflow", pk_checkflow);
                        if (!StringUtils.isBlank(flownodeid)) {
                            pointMap.put("flownodeid", flownodeid);
                            pointMap.put("billtypecode", json.getString("billtype"));
                            pointMapList.add(pointMap);
                        }
                    }
                }
            }

            result.put("items", msgs);
            int count = iMessageQueryService.queryNCMessageCount(params);
            result.put("total", count);
            result.put("currentTime", ((ITimeService)ServiceLocator.find(ITimeService.class)).getUFDateTime());
            result.put("billTypeToModuleMap", this.getModuleBybillTypeSet(billTypeSet));
            Map<String, String> defbuttonName = this.listGetButtonName();
            result.put("curlanguage", MultiLangUtil.getCurrentLangSeq());
            result.put("defbuttonName", defbuttonName);
        } catch (BusinessException var23) {
            Logger.error(var23.getMessage(), var23);
        }

        return result;
    }

    private Map<String, String> listGetButtonName() throws BusinessException {
        IApproveButtonService service = (IApproveButtonService)ServiceLocator.find(IApproveButtonService.class);
        ApproveButtonSettingVO[] ret = service.loadButtonSetting(SessionContext.getInstance().getClientInfo().getPk_group());
        Map<String, String> defbuttonName = new HashMap();
        if (ret != null && ret.length > 0) {
            for(int i = 0; i < ret.length; ++i) {
                if (i == 0) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0000"));
                    }
                } else if (i == 1) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0001"));
                    }
                } else if (i == 2) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0002"));
                    }
                } else if (i == 3) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0003"));
                    }
                } else if (i == 4) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put("billmake", ret[i].getButtonname());
                    } else {
                        defbuttonName.put("billmake", NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0004"));
                    }
                } else if (i == 5) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put("before", ret[i].getButtonname());
                    } else {
                        defbuttonName.put("before", NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0005"));
                    }
                } else if (i == 6) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0006"));
                    }
                } else if (i == 7) {
                    if (ret[i].getButtonname() != null && !"".equals(ret[i].getButtonname())) {
                        defbuttonName.put(ret[i].getButtoncode(), ret[i].getButtonname());
                    } else {
                        defbuttonName.put(ret[i].getButtoncode(), NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow65", "ApproveButtonName-0007"));
                    }
                }
            }
        }

        return defbuttonName;
    }

    private Map<String, String> getModuleBybillTypeSet(Set<String> billTypeSet) throws BusinessException {
        IPFMetaModel pFMetaModelImpl = (IPFMetaModel)ServiceLocator.find(IPFMetaModel.class);
        Map<String, String> map = pFMetaModelImpl.queryModuleOfBilltypeToMap(billTypeSet);
        return (Map)(MapUtils.isEmpty(map) ? new HashMap() : map);
    }

    private String getParam(String params, String paramName, String Separator) {
        int start = 1;
        if (params != null && (start = params.indexOf(paramName)) > -1) {
            start = start + paramName.length() + 1;
            int end = params.indexOf(Separator, start);
            if (end < start) {
                end = params.length();
            }

            String param = params.substring(start, end);
            return param;
        } else {
            return null;
        }
    }

    private String delParam(String params, String paramName, String Separator) {
        String[] paramArray = params.split(Separator);
        StringBuilder result = new StringBuilder();
        String[] var6 = paramArray;
        int var7 = paramArray.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String param = var6[var8];
            if (param != null && !"".equals(param) && param.indexOf(paramName) != 0) {
                result.append(param);
                result.append("@");
            }
        }

        paramArray = null;
        result.deleteCharAt(result.length() - 1);
        return result.toString();
    }
}
